<template>
    <view>
        <view v-if="(info || null) != null">
            <form @submit="form_submit" class="form-container">
                <view class="page-bottom-fixed padding-main">
					<view class="padding-main">
						<view class="cr-red">您可以在此管理商品分类</view>
					</view> 
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>分类名称</view>
								<input type="text" placeholder-class="cr-grey-9" placeholder="输入分类名称" name="name" class="cr-base flex-1 flex-width tr" :value="info.name || ''" maxlength="20" />
					        </view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>{{$t('form.form.043a10')}}</view>
					            <view class="flex-1 flex-width tr">
					                <switch :color="diytheme.main_color" :checked="(info.is_enable || 0) == 1 ? true : false" @change="is_enable_event" />
					            </view>
					        </view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>展示在店铺</view>
					            <view class="flex-1 flex-width tr">
					                <switch :color="diytheme.main_color" :checked="(info.is_show || 0) == 1 ? true : false" @change="is_show_event" />
					            </view>
					        </view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb spacing-mt">
					    <view class="form-gorup">
					        <view class="form-gorup-title padding-right-main fw-b"><text class="cr-red text-size-xs margin-right-sm"></text>图标</view>
							<view class="margin-top-main">
								<component-upload :propData="image_list" :propMaxNum="1" propDelServ="yes" :propDelServUrl="del_serv_url" :propPathType="editor_path_type" @call-back="retrun_image_event"></component-upload>
							</view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>排序</view>
								<input type="number" placeholder-class="cr-grey-9" placeholder="输入排序序号" name="sort" class="cr-base flex-1 flex-width tr" :value="info.sort || 0" min="0" />
					        </view>
					    </view>
					</view>
                </view>
                <!-- 底部操作 -->
                <view class="bottom-fixed btn-bottom bg-white">
                    <view class="oh bottom-line-exclude">
                        <button class="cr-white round text-size wh-auto" type="default" form-type="submit" hover-class="none" :loading="form_submit_loading" :disabled="form_submit_loading" :style="'background:linear-gradient(107deg, ' + diytheme.main_color + ' 0%, ' + diytheme.main_color + ' 100%)'">{{$t('common.submit')}}</button>
                    </view>
                </view>
            </form>
        </view>
        <view v-else>
            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </view>

		<!-- #ifdef APP-PLUS -->
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
		<!-- #endif -->
    </view>
</template>
<script>
    const app = getApp(); 
	// #ifdef APP-PLUS
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	// #endif
    import componentPopup from '@/components/popup/popup';
    import componentUpload from '@/components/upload/upload';
    import componentNoData from '@/components/no-data/no-data';
    export default {
        data() {
            return {
                theme_view: app.globalData.get_theme_value_view(),
                theme_color: app.globalData.get_theme_color(),
                data_list_loding_status: 1,
				data_list_loding_msg: '',
				params:null,
				info:null,
				config: null,
				user: null,

                // 封面图片
                image_list: [],
                // 路径类型
                editor_path_type: 'common',
				del_serv_url: app.globalData.get_request_url("deleteimg", "info", "classinfo"),
				
                // 内容
                input_length_max: 500,
                input_value: '',

                // 提交按钮
                form_submit_loading: false,
				
				diytheme: app.globalData.get_fcc_theme_color(),
            };
        },
        components: {
			// #ifdef APP-PLUS
			ykAuthpup,
			// #endif
            componentPopup,
            componentUpload,
            componentNoData
        },

        onLoad(params) {
			this.setData({
			    params: params
			});
			
            // 数据加载
            this.init();
        },

        onShow() {
			app.globalData.page_event_onshow_handle();
		},

        // 下拉刷新
        onPullDownRefresh() {
			this.init();
		},

        methods: {
            init() {
                var user = app.globalData.get_user_info(this, 'init');
                if (user != false) {
					console.log(user);
					this.setData({
					    user: user
					});
                    this.get_data();
                } else {
                    // 提示错误
                    this.setData({
                        data_list_loding_status: 2,
                        data_list_loding_msg: this.$t('form.form.8l3ul5'),
                    });
                }
            },
            get_data() {
                // 加载loding
                uni.showLoading({
                    title: this.$t('common.loading_in_text'),
                });
				
				this.setData({
				    data_list_loding_status: 1,
				});
				
				var id = this.params.id || 0; 
				
                uni.request({
                    url: app.globalData.get_request_url('index', 'product', 'classinfo'),
                    method: 'POST',
                    data: {id: id},
                    success: (res) => {
                        uni.hideLoading();
                        if (res.data.code == 0) {
							var info = res.data.data.data || null;
							var base = res.data.data.base || null;
							var image_list = [];
							if((info.icon || null) != null && info.icon != '')
							{
								image_list.push(info.icon);
							}
							this.setData({
							    data_bottom_line_status: true,
							    data_list_loding_status: 3,
							    info: info,
								config: base,
								image_list: image_list,
							});
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
								data_list_loding_msg: res.data.msg,
                            });
                            if (app.globalData.is_login_check(res.data, this, 'get_data')) {
                                app.globalData.showToast(res.data.msg);
                            }
                        }
                    },
                    fail: () => {
                        uni.hideLoading();
                        this.setData({
                            data_list_loding_status: 2,
							data_list_loding_msg: this.$t('common.internet_error_tips')
                        });
                        app.globalData.showToast(this.$t('common.internet_error_tips'));
                    },
                });
            },

            // 上传回调
            retrun_image_event(data) {
                var new_data = this.info;
                new_data.icon = data[0];
				var image_list = [];
				image_list.push(data[0]);
                this.setData({
                    info: new_data,
					image_list: image_list,
                });
            },
			
			// 是否启用
			is_enable_event(e) {
			    var new_data = this.info;
			    new_data.is_enable = e.detail.value ? 1 : 0;
			    this.setData({
			        info: new_data,
			    });
			},
			
			// 是否展示
			is_show_event(e) {
			    var new_data = this.info;
			    new_data.is_show = e.detail.value ? 1 : 0;
			    this.setData({
			        info: new_data,
			    });
			},
			
            // 表单提交
            form_submit(e) {
                // 数据验证
                var validation = [
                    { fields: 'name', msg: '名称不能为空' },
                ];
				var validate = {
				    name: e.detail.value.name,
				};
                if (app.globalData.fields_check(validate, validation)) {
                    uni.showLoading({
                        title: this.$t('buy.buy.r79t77'),
                    });
                    this.setData({
                        form_submit_loading: true,
                    });
					
					var temp_info = this.info;
					temp_info['name'] = e.detail.value.name;
					temp_info['sort'] = e.detail.value.sort;

                    // 网络请求
                    uni.request({
                        url: app.globalData.get_request_url('categorysave', 'product', 'classinfo'),
                        method: 'POST',
                        data: temp_info,
                        dataType: 'json',
                        success: (res) => {
                            uni.hideLoading();
                            if (res.data.code == 0) {
                                app.globalData.showToast(res.data.msg, 'success');
                                setTimeout(function () {
                                    app.globalData.page_back_prev_event();
                                }, 2000);
                            } else {
                                this.setData({
                                    form_submit_loading: false,
                                });
                                if (app.globalData.is_login_check(res.data)) {
                                    app.globalData.showToast(res.data.msg);
                                } else {
                                    app.globalData.showToast(this.$t('common.sub_error_retry_tips'));
                                }
                            }
                        },
                        fail: () => {
                            uni.hideLoading();
                            this.setData({
                                form_submit_loading: false,
                            });
                            app.globalData.showToast(this.$t('common.internet_error_tips'));
                        },
                    });
                }
            }
        },
    };
</script>
<style>

</style>
